package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.NcgService;
import com.sqx.modules.health.entity.ServicetypeByHealth;
import com.sqx.modules.health.service.INcgServiceService;
import com.sqx.modules.health.service.IServicetypeService;
import com.sqx.modules.health.utils.MyBeanUtil;
import com.sqx.modules.health.vo.NcgServiceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 服务项目 前端控制器
 * </p>
 *
 * @author gxy
 * @since 2022-04-08
 */

@Api(value = "服务项目", tags = "服务项目", description = "")
@RestController
@RequestMapping("/health/ncgService")
public class NcgServiceController {
    @Autowired
    private INcgServiceService ncgServiceService;

    @Autowired
    private IServicetypeService servicetypeService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新服务项目", notes = "新增或更新服务项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "服务项目id", paramType = "Integer"),
            @ApiImplicitParam(name = "name", value = "服务名称", paramType = "String"),
            @ApiImplicitParam(name = "duration", value = "时长", paramType = "decimal"),
            @ApiImplicitParam(name = "cost", value = "费用", paramType = "decimal"),
            @ApiImplicitParam(name = "materialcost", value = "材料费用", paramType = "decimal"),
            @ApiImplicitParam(name = "details", value = "详情", paramType = "String"),
            @ApiImplicitParam(name = "contact", value = "解除", paramType = "Integer"),
            @ApiImplicitParam(name = "examine", value = "检查", paramType = "Integer"),
            @ApiImplicitParam(name = "cmcc", value = "包月", paramType = "Integer"),
            @ApiImplicitParam(name = "remarks", value = "备注", paramType = "String"),
    })
    public Result save(@RequestBody NcgService ncgService) {
        return Result.success(ncgServiceService.saveOrUpdate(ncgService));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除服务项目", notes = "根据ID删除服务项目")
    public Result delete(@PathVariable Integer id) {
        return Result.success(ncgServiceService.removeById(id));
    }

    // 根据IDS删除
    @GetMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除服务项目", notes = "根据IDS删除服务项目")
    public Result deleteBatch(@RequestParam List<Integer> ids) {
        return Result.success(ncgServiceService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部服务项目", notes = "查询全部服务项目")
    public Result findAll(@RequestParam(required = false, defaultValue = "") Long hid) {
        QueryWrapper<NcgService> wrapper = new QueryWrapper<NcgService>()
                .eq(ObjectUtil.isNotEmpty(hid), "hid", hid); // .eq("isable", 0)
        return Result.success(ncgServiceService.list(wrapper));
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录服务项目", notes = "根据ID查询一条记录服务项目")
    public Result findOne(@RequestParam Integer id) {
        return Result.success(ncgServiceService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询服务项目", notes = "分页查询服务项目")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false, defaultValue = "") String name,
                           @RequestParam(required = false, defaultValue = "") Long hid) {
        QueryWrapper<NcgService> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        if (ObjectUtil.isNotEmpty(hid)) {
            queryWrapper.eq("hid", hid);
        }
        queryWrapper.orderByDesc("id");
        Page<NcgService> page = ncgServiceService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<NcgServiceVo> ncgServiceVos = MyBeanUtil.collectionCopy(page.getRecords(), NcgServiceVo.class);
        ncgServiceVos.forEach(item -> {
            ServicetypeByHealth servicetypeByHealth = servicetypeService.getById(item.getTypeid());
            item.setTypeName(servicetypeByHealth.getServiceType());
        });
        Page<NcgServiceVo> result = new Page<>();
        result.setRecords(ncgServiceVos);
        result.setTotal(page.getTotal());
        return Result.success(result);
    }

    @GetMapping("/showService")
    @ApiOperation(value = "小程序首页服务项目查询接口", notes = "小程序首页服务项目查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hid", value = "医院id", paramType = "Long"),
    })
    public Result showService(@RequestParam(required = false, defaultValue = "") Long hid,
                              @RequestParam(required = false, defaultValue = "") Long rec) {
        QueryWrapper<NcgService> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (rec == null) {
            queryWrapper.eq("isrec", 0);
        }
        queryWrapper.eq("isable", 0); // 查询所有启用项
        queryWrapper.eq("hid", hid);
        List<NcgService> result = null;
        List<NcgService> populars = ncgServiceService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(populars)) {
            result = populars;
        } else {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");
            queryWrapper.eq("hid", hid);
            result = ncgServiceService.list(queryWrapper);
        }
        return Result.success(result);
    }


    @GetMapping("/getServiceByName")
    @ApiOperation(value = "小程序首页按照项目名称模糊查询接口", notes = "小程序首页服务项目查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceName", value = "输入参数", paramType = "String"),
            @ApiImplicitParam(name = "hid", value = "医院id", paramType = "Long"),
    })
    public Result getServiceByName(@RequestParam String serviceName, @RequestParam Long hid) {
        if (ObjectUtil.isEmpty(serviceName) || ObjectUtil.isEmpty(hid)) {
            return Result.error(Constants.CODE_500, "参数错误");
        }
        QueryWrapper<NcgService> wrapper = new QueryWrapper<NcgService>()
                .like(ObjectUtil.isNotEmpty(serviceName), "name", serviceName)
                .eq("isable", 0)
                .eq(ObjectUtil.isNotEmpty(hid), "hid", hid);
        return Result.success(ncgServiceService.list(wrapper));
    }

}

